package dao

import (
	"gorm.io/gorm"
	"view-crawler-backend/internal/dao/entity"
)

func BatchAddTaskCategory(db *gorm.DB, taskCategoryList []entity.TaskCategory) error {
	return db.Table("task_category").Create(&taskCategoryList).Error
}

func QueryCategoryIdListByTaskId(db *gorm.DB, taskId int64) ([]int64, error) {
	var categoryList []entity.Category
	if err := db.Table("task_category").Select("category_id").Where("task_id =?", taskId).Find(&categoryList).Error; err != nil {
		return nil, err
	}
	categoryIdList := make([]int64, len(categoryList))
	for i, category := range categoryList {
		categoryIdList[i] = category.CategoryId
	}
	return categoryIdList, nil
}

func DeleteTaskCategoryByTaskId(db *gorm.DB, taskId int64) error {
	return db.Table("task_category").Where("task_id =?", taskId).Delete(&entity.TaskCategory{}).Error
}
